package org.derusse.cycle.activity;

import java.util.ArrayList;

import org.derusse.cycle.CycleMeterApplication;
import org.derusse.cycle.R;
import org.derusse.cycle.database.PointTable;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.widget.TabHost;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class TrackViewerActivity extends MapActivity {
	private CycleMeterApplication application;
	private MapView mapView;
	private MyLocationOverlay mMyLocationOverlay;
	private TrackOverlay trackOverlay;

	private long trackId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.track_view);

		this.application = (CycleMeterApplication) getApplication();
		this.trackId = this.application.trackView;

		TabHost tabs = (TabHost) findViewById(R.id.tabhost);

		tabs.setup();

		tabs.addTab(tabs.newTabSpec("tab_1_btn_tab").setIndicator(
				getString(R.string.tab_view_main)).setContent(
				R.id.ViewIncludeMain));
		tabs.addTab(tabs.newTabSpec("tab_2_btn_tab").setIndicator(
				getString(R.string.tab_view_map)).setContent(
				R.id.ViewIncludeMap));
		tabs.addTab(tabs.newTabSpec("tab_3_btn_tab").setIndicator(
				getString(R.string.tab_view_stats)).setContent(
				R.id.ViewIncludeStats));

		tabs.setCurrentTab(1);

		mapView = (MapView) findViewById(R.id.TrackMapView);

		mMyLocationOverlay = new MyLocationOverlay(this, mapView);
		mapView.getOverlays().add(mMyLocationOverlay);

		Cursor cursor = null;
		SQLiteDatabase db = null;
		ArrayList<GeoPoint> geoPoints = new ArrayList<GeoPoint>();

		Point max = null;
		Point min = null;

		try {
			db = this.application.getDatabaseHelper().getReadableDatabase();
			cursor = PointTable.ListCoordinates(db, this.trackId);

			if (cursor != null) {
				if (cursor.moveToFirst()) {
					do {
						int lat = ((Double) (cursor.getDouble(0) * 1E6))
								.intValue();
						int lon = ((Double) (cursor.getDouble(1) * 1E6))
								.intValue();

						if (max == null)
							max = new Point(lat, lon);
						else {
							max.x = Math.max(max.x, lat);
							max.y = Math.max(max.y, lon);
						}
						if (min == null)
							min = new Point(lat, lon);
						else {
							min.x = Math.min(min.x, lat);
							min.y = Math.min(min.y, lon);
						}
						geoPoints.add(new GeoPoint(lat, lon));

						cursor.moveToNext();
					} while (!cursor.isLast());
				}
			}
		} finally {
			cursor.close();
			db.close();
		}

		if (max != null) {
			mapView.getController().setCenter(new GeoPoint((max.x+min.x)/2, (max.y+min.y)/2));
			int dist = Math.max(max.x-min.x, max.y-min.y);
			
			mapView.getController().setZoom(Math.min(19, 21-dist/5000));
		}
		
		trackOverlay = new TrackOverlay(this, mapView, geoPoints);

		mapView.getOverlays().add(trackOverlay);

		mapView.setClickable(true);
		mapView.setEnabled(true);
		mapView.setBuiltInZoomControls(true);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	protected class TrackOverlay extends Overlay {

		ArrayList<GeoPoint> geoPoints = null;
		Bitmap flagEnd;
			Bitmap flagStart;

		public TrackOverlay(MapActivity activity, MapView mapView,
				ArrayList<GeoPoint> geoPoints) {
			this.geoPoints = geoPoints;
			flagEnd = BitmapFactory.decodeResource(getResources(), R.drawable.flag_end);
			flagStart = BitmapFactory.decodeResource(getResources(), R.drawable.flag_start);
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);

			int countPoints = geoPoints.size();
			if (countPoints < 1)
				return;
			
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setColor(Color.BLUE);
			paint.setStrokeWidth(2);

			Projection projection = mapView.getProjection();
			Point p1 = new Point();
			Point p2 = new Point();
			
			projection.toPixels(this.geoPoints.get(0), p1);
			canvas.drawBitmap(flagStart, p1.x-4, p1.y-30, paint);

			if (countPoints < 2)
				return;

			projection.toPixels(this.geoPoints.get(0), p1);

			for (int i = 1; i < countPoints; i++) {
				projection.toPixels(this.geoPoints.get(i), p2);
				canvas.drawLine(p1.x, p1.y, p2.x, p2.y, paint);
				p1 = new Point(p2);
			}

			projection.toPixels(this.geoPoints.get(countPoints-1), p1);
			canvas.drawBitmap(flagEnd, p1.x-4, p1.y-30, paint);
		}
	}
}